﻿using System;
using System.Data;
using System.Text;
using System.Data.OleDb;
using Maticsoft.DBUtility;
namespace Maticsoft
{
    /// <summary>
    /// 类News。
    /// </summary>
    [Serializable]
    public partial class News
    {
        public News()
        { }
        #region Model
        private int _id;
        private string _ntitle;
        private int? _ntype;
        private string _ncontent;
        private DateTime? _ndate;
        /// <summary>
        /// 
        /// </summary>
        public int ID
        {
            set { _id = value; }
            get { return _id; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string NTITLE
        {
            set { _ntitle = value; }
            get { return _ntitle; }
        }
        /// <summary>
        /// 
        /// </summary>
        public int? NTYPE
        {
            set { _ntype = value; }
            get { return _ntype; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string NCONTENT
        {
            set { _ncontent = value; }
            get { return _ncontent; }
        }
        /// <summary>
        /// 
        /// </summary>
        public DateTime? NDATE
        {
            set { _ndate = value; }
            get { return _ndate; }
        }
        #endregion Model


        #region  Method

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(String title, String id, String tid)
        {
            OleDbDataReader dr = null;
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("select NTITLE from [News]");
                strSql.Append(" where NTITLE=@NTITLE ");
                if (id != "-1")
                {
                    strSql.Append(" and ID <> " + id + " and TTYPE <> " + tid);
                }
                OleDbParameter[] parameters = {
					new OleDbParameter("@NTITLE", OleDbType.Integer,4)};
                parameters[0].Value = title;

                dr = DbHelperOleDb.ExecuteReader(strSql.ToString(), parameters);
                return dr.HasRows;
            }
            catch (Exception ex) { return true; }
            finally
            {
                dr.Close();
                dr.Dispose();
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public Boolean Add()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into [News] (");
            strSql.Append("NTITLE,NTYPE,NCONTENT,NDATE)");
            strSql.Append(" values (");
            strSql.Append("@NTITLE,@NTYPE,@NCONTENT,@NDATE)");
            OleDbParameter[] parameters = {
					new OleDbParameter("@NTITLE", OleDbType.VarChar,255),
					new OleDbParameter("@NTYPE", OleDbType.Integer,4),
					new OleDbParameter("@NCONTENT", OleDbType.VarChar,0),
					new OleDbParameter("@NDATE", OleDbType.Date)};
            parameters[0].Value = NTITLE;
            parameters[1].Value = NTYPE;
            parameters[2].Value = NCONTENT;
            parameters[3].Value = NDATE;

            return DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters) > 0;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update [News] set ");
            strSql.Append("NTITLE=@NTITLE,");
            strSql.Append("NTYPE=@NTYPE,");
            strSql.Append("NDATE=@NDATE,");
            strSql.Append("NCONTENT=@NCONTENT");
            strSql.Append(" where ID=@ID ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@NTITLE", OleDbType.VarChar,255),
					new OleDbParameter("@NTYPE", OleDbType.Integer,4),
                    new OleDbParameter("@NDATE", OleDbType.Date),
					new OleDbParameter("@NCONTENT", OleDbType.VarChar,0),
					new OleDbParameter("@ID", OleDbType.Integer,4)};
            parameters[0].Value = NTITLE;
            parameters[1].Value = NTYPE;
            parameters[2].Value = NDATE;
            parameters[3].Value = NCONTENT;
            parameters[4].Value = ID;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [News] ");
            strSql.Append(" where ID=@ID ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@ID", OleDbType.Integer,4)};
            parameters[0].Value = ID;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public void GetModel(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,NTITLE,NTYPE,NCONTENT,NDATE ");
            strSql.Append(" FROM [News] ");
            strSql.Append(" where ID=@ID ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@ID", OleDbType.Integer,4)};
            parameters[0].Value = ID;

            DataSet ds = DbHelperOleDb.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    this.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["NTITLE"] != null && ds.Tables[0].Rows[0]["NTITLE"].ToString() != "")
                {
                    this.NTITLE = ds.Tables[0].Rows[0]["NTITLE"].ToString();
                }
                if (ds.Tables[0].Rows[0]["NTYPE"] != null && ds.Tables[0].Rows[0]["NTYPE"].ToString() != "")
                {
                    this.NTYPE = int.Parse(ds.Tables[0].Rows[0]["NTYPE"].ToString());
                }
                if (ds.Tables[0].Rows[0]["NCONTENT"] != null && ds.Tables[0].Rows[0]["NCONTENT"].ToString() != "")
                {
                    this.NCONTENT = ds.Tables[0].Rows[0]["NCONTENT"].ToString();
                }
                if (ds.Tables[0].Rows[0]["NDATE"] != null && ds.Tables[0].Rows[0]["NDATE"].ToString() != "")
                {
                    this.NDATE = DateTime.Parse(ds.Tables[0].Rows[0]["NDATE"].ToString());
                }
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select n.ID,n.NTITLE,n.NDATE,c.TNAME as NTYPE ");
            strSql.Append(" FROM [News] as n left join Categorys as  c on n.NTYPE=c.ID ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere + " order by NDATE DESC");
            }
            return DbHelperOleDb.Query(strSql.ToString());
        }
        public String GetNewsType_V()
        {
            String sql = "select c.TNAME,count(n.NTYPE) as SSUM FROM [News] as n left join Categorys as  c on n.NTYPE=c.ID group by TNAME";

            DataSet ds = DbHelperOleDb.Query(sql.ToString());
            if (ds != null && ds.Tables.Count != 0)
            {
                StringBuilder json = new StringBuilder();
                json.Append("[");
                DataTable dt = ds.Tables[0];
                for (Int32 i = 0; i < dt.Rows.Count; i++)
                {
                    json.Append("[");
                    json.AppendFormat("'{0}','{1}'", dt.Rows[i]["TNAME"], dt.Rows[i]["SSUM"]);
                    json.Append("]");
                    if (i != (dt.Rows.Count - 1))
                    {
                        json.Append(",");
                    }
                }
                json.Append("]");
                return json.ToString();
            }
            return "[]";
        }
        public String GetList_V(String top, String type)
        {
            StringBuilder strSql = new StringBuilder();
            if (top.Trim() != "")
            {
                strSql.Append("select top " + top + " n.ID,n.NTITLE,n.NDATE,c.TNAME as NTYPE ");
            }
            else
            {
                strSql.Append("select n.ID,n.NTITLE,n.NDATE,c.TNAME as NTYPE ");
            }
            strSql.Append(" FROM [News] as n left join Categorys as  c on n.NTYPE=c.ID ");
            if (type.Trim() != "")
            {
                strSql.Append(" where PTYPE= " + type);
            }
            strSql.Append(" order by NDATE DESC");
            DataSet ds = DbHelperOleDb.Query(strSql.ToString());
            if (ds != null && ds.Tables.Count != 0)
            {
                StringBuilder json = new StringBuilder();
                json.Append("[");
                DataTable dt = ds.Tables[0];
                for (Int32 i = 0; i < dt.Rows.Count; i++)
                {
                    json.Append("[");
                    json.AppendFormat("'{0}','{1}','{2}','{3}'", dt.Rows[i]["ID"], dt.Rows[i]["NTYPE"], dt.Rows[i]["NTITLE"], Convert.ToDateTime(dt.Rows[i]["NDATE"]).ToString("yyyy-MM-dd"));
                    json.Append("]");
                    if (i != (dt.Rows.Count - 1))
                    {
                        json.Append(",");
                    }
                }
                json.Append("]");
                return json.ToString();
            }
            return "[]";
        }
        public String GetList(int page, int rows, Maticsoft.Query query)
        {
            StringBuilder str = new StringBuilder();
            str.Append(" NDATE >= #" + query.from + "# and NDATE <= #" + query.to + "#");
            if (query.name != "")
            {
                str.Append(" and TTITLE %" + query.name + "%");
            }
            if (query.type != "")
            {
                str.Append(" and type in (" + query.type + ") ");
            }
            DataSet ds = GetList(str.ToString());
            if (ds != null && ds.Tables.Count != 0)
            {
                DataTable dt = ds.Tables[0];
                if (dt != null && dt.Rows.Count != 0)
                {
                    DataTable newDt = dt.Clone();
                    int rowCount = dt.Rows.Count - dt.Rows.Count % rows == rows * (page - 1) ? dt.Rows.Count : page * rows;
                    for (int i = rows * (page - 1); i < rowCount; i++)
                    {
                        Object[] dr = dt.Rows[i].ItemArray;
                        newDt.Rows.Add(dr);
                    }
                    return "{\"total\":" + dt.Rows.Count + ",\"rows\":" + DataTableExt.DataTableToJson(newDt).ToString() + "}";
                }
                return "{\"total\":0,\"rows\":[]}";
            }
            return "{\"total\":0,\"rows\":[]}";
        }
        #endregion  Method
    }
}

